package servlet;

import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.digest.Md5Crypt;
import service.LoginService;
import serviceImpl.LoginServiceImpl;
import util.Md5;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "LoginServlet",urlPatterns = "/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out =response.getWriter();
         String opr=request.getParameter("opr");
         if("login".equals(opr)){
             String name=request.getParameter("name");
             String pwd=request.getParameter("pwd");

             int num=new LoginServiceImpl().doLogin(name, Md5.getMD5Str(pwd));
             System.out.println(name+"-----"+pwd+"---"+num);
             if(num>0) {
                 request.getRequestDispatcher("/Index.jsp").forward(request, response);

             }else{
                 response.sendRedirect("Login.jsp");
                /* out.print("该用户不存在哦!");
                 out.flush();
                 out.close();*/
             }
         } else if("regist".equals(opr)){
             String name=request.getParameter("name");
             String pwd=request.getParameter("pwd");
             String realname=request.getParameter("realname");
             String sex=request.getParameter("sex");
             String userid=request.getParameter("userid");
             String email=request.getParameter("email");
             String tel=request.getParameter("tel");
             System.out.println(name+"--"+pwd+"---"+realname+"--"+sex+"--"+userid+"---"+email+"--"+tel);



         }

    }
}
